//
//  AddServiceTaskViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/5/8.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire
class AddServiceTaskViewController: BaseViewController {

    @IBOutlet var releaseBtn: UIButton!
    
    @IBOutlet var budgetTF: UITextField!
    
    @IBOutlet var needsLabel: UIButton!
    
    @IBOutlet var contentTF: UITextField!
    
    var needsTypes :[NeedsTypeModel]?
    var needsTypeId = ""
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        requestPlatformTaskType()

    }

    @IBAction func needsBtnPress(_ sender: UIButton) {
        if (needsTypes?.count)! > 0 {
            var Ids = [String]()
            var Names = [String]()
            for (type) in needsTypes! {
                Names.append(type.name)
                Ids.append(type.id)
            }
            self.view.endEditing(true)
            BRStringPickerView.showStringPicker(withTitle: "需求类型", dataSource: Names, defaultSelValue: Names[0], isAutoSelect: true) { (str,index) in
                self.needsLabel.setTitle(str as? String, for: .normal)
                self.needsTypeId = Ids[index]
            }
        }
    }
    
    
    @IBAction func releaseBtnPress(_ sender: UIButton) {
        
        if budgetTF.text! == "" || contentTF.text! == ""{
            MBProgressHUD.alertShow(in: self.view, message: "请填写完整再发布", hidenAfter: 0.8, mode: MBProgressHUDMode.text)
            return
        }
        if needsTypeId == "" {
            MBProgressHUD.alertShow(in: self.view, message: "请选择需求类型", hidenAfter: 0.8, mode: MBProgressHUDMode.text)
            return
        }
        
        requestAddPlatformTask(taskType: needsTypeId, budgetMoney: budgetTF.text!, content: contentTF.text!)
    }
    
    
    //网络请求
    //发布需求
    func requestAddPlatformTask(taskType:String,budgetMoney:String,content:String) -> Void {
        let param =
            ["task_type":taskType,
             "budget_money":budgetMoney,
             "describe":content,
             "flag":"2",//区别用户端和商户端 1用户端  2 商户端
             "uid":UserModel.shareUser.data?.uid ?? String(),
             ] as [String : Any]
        
        Alamofire.request(releasePlatformTaskUrl, method:.post, parameters:param)
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    
                    var basemodel = DefaultData()
                    basemodel = DefaultData.deserialize(from: response.value)!
                    
                    switch basemodel.status! {
                    case true:
                        let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
                        hud?.mode = MBProgressHUDMode.text
                        hud?.labelText = "发布成功"
                        hud?.show(animated: true, whileExecuting: {                        Thread.sleep(forTimeInterval: 0.8)
                        }, completionBlock: {                    self.navigationController?.popViewController(animated: true)
                        })
                    case false:
                        MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                    
                case false:
                    print(response.result.error!)
                }
                
        }
    }

    
    //获取需求类型
    func requestPlatformTaskType() -> Void {
        
        
        Alamofire.request(getPlatformTaskTypeUrl, method:.post, parameters:nil)
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    
                    var basemodel = BaseResponseOfModel<BaseArrayData<NeedsTypeModel>>()
                    basemodel = BaseResponseOfModel.deserialize(from: response.value)!
                    switch basemodel.status! {
                    case true:
                         self.needsTypes = basemodel.data?.data
                        print("数据请求成功")
                    case false:
                         MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                    
                case false:
                    print(response.result.error!)
                }
                
        }
    }
    
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
